<?php

/**
 * @file formatted_table.php
 * @brief Clase para generar una tabla con formato
 * @author DyrTechnologies
 * @version V 1.0
 * @date Wed Oct 03 2007
 *
 *  Part of the CEFIRO dynamic web framework project
 *
 *  Copyright (C) 2007  Dyr technologies http://dyrtech.com.ve
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @class Clase formatted_table
 */



/**
 * @brief Define la ruta a la hoja de estilos para la tabla con formatos
 *
 */

define("FORMATTED_TABLE_CSS",FULL_URL."formatted_table/css/formatted_table.css");
define("FORMATTED_TABLE_JS",FULL_URL."formatted_table/js/formatted_table.js");

$formatted_table_instance = 0;

class formatted_table {

	private $matrixContent;
	private $arrayTitle;
	private $arrayKeys;
	private $arrayID;
	private $rowOpts;
	private $name;
	private $target;
	private $link;
	private $nameid;
	private $extraOpt;
	private $linkOpt;
	private $divsize;

	/**
	 * @brief Constructor por omision y parametrico de la clase aprovechamos el hecho de que cada atributo tiene un valor por omision predefinido para no rehacer el constructor
	 *
	 * @param matrix( n * size( $arrayKeys ) ) $_matrixContent
	 * @param array $_arrayTitle
	 * @param array $_arrayKeys
	 * @param array $_rowOpts
	 * @param $_name
	 * @param $_target
	 * @param $_link
	 * @param $_nameid
	 * @param $_linkOpt
	 * @return formatted_table La instancia de la clase
	 */

	public function formatted_table($_matrixContent = null, $_arrayTitle = null, $_arrayKeys = null, $_rowOpts = null, $_name = "__formatted_table", $_target = 'self', $_link = '', $_nameid = 'id', $_linkOpt = '', $_divsize = '', $_extraOpt = ''){


		/**
		 * @brief Se asignan los valores a los atributos de la clase radio segun vengan dados al llamar al constructor de la misma, notar que existen valores por omision para cada atributo
		 */
		$this->matrixContent = $_matrixContent;
		$this->arrayTitle = $_arrayTitle;
		$this->rowOpts = $_rowOpts;
		$this->target = $_target;
		$this->link = $_link;
		$this->nameid = $_nameid;
		$this->extraOpt = $_extraOpt;
		$this->linkOpt = $_linkOpt;

		/**
		 * @brief Si no esta ajustado el arreglo de claves se ajusta como el primer arreglo de la matriz con los contenidos
		 */
		if($_arrayKeys == null)
		$_arrayKeys = @_array_keys($this->matrixContent[0]);

		$this->arrayKeys = $_arrayKeys;

		/**
		 * @brief $radio_instance es una variable para incrementar el numero de botones radio existentes
		 */
		global $formatted_table_instance;
		$formatted_table_instance++;

		/**
		 * @brief El nombre del widget es el tipo con el numero de la instancia que representa o el nombre pasado por omision en caso contrario
		 */

		if($_name!="__formatted_table")
			$this->name=$_name;
		else
			$this->name="formatted_table".$formatted_table_instance;

		$this->divsize = $_divsize;
	}


	/**
	 * @brief Las funciones get_VAR devuelven las variables que tenga ajustadas la instancia: Contenido (matrixContent), Titulos (arrayTitle), Claves (arrayKeys), opciones de entrada (rowOpts)
	 *
	 * @return Valor de VAR
	 */

	public function get_content(){
		return $this->matrixContent;
	}

	public function get_titles(){
		return $this->arrayTitle;
	}

	public function get_keys(){
		return $this->arrayKeys;
	}

	public function get_opt(){
		return $this->rowOpts;
	}

	public function get_name(){
		return $this->name;
	}

	public function get_target(){
		return $this->target;
	}

	public function get_link(){
		return $this->link;
	}

	public function get_nameid(){
		return $this->nameid;
	}

	public function get_extraOpt(){
		return $this->extraOpt;
	}

	public function get_linkOpt(){
		return $this->linkOpt;
	}

	public function get_scripts(){
		return FORMATTED_TABLE_JS;
	}

	public function get_divsize(){
		return $this->divsize;
	}

	/**
	 * @brief Las funciones set_VAR ajustan las variables de la instancia: Contenido (matrixContent), Titulos (arrayTitle), Claves (arrayKeys), opciones de entrada (rowOpts)
	 */

	public function set_content($_matrixContent){
		$this->matrixContent=$_matrixContent;
	}

	public function set_titles($_arrayTitle){
		$this->arrayTitle=$_arrayTitle;
	}

	public function set_keys($_arrayKeys =  null){
		if($_arrayKeys == null)
		$_arrayKeys = @_array_keys($this->matrixContent[0]);

		$this->arrayKeys = $_arrayKeys;
	}

	public function set_name($_name){
		$this->name=$_name;
	}

	public function set_opt($_RowOpts){
		$this->rowOpts=$_RowOpts;
	}

	public function set_target($_target){
		$this->target=$_target;
	}

	public function set_link($_link){
		$this->link=$_link;
	}

	public function set_nameid($_nameid){
		$this->nameid=$_nameid;
	}

	public function set_extraOpt($_extraOpt){
		$this->extraOpt=$_extraOpt;
	}

	public function set_linkOpt($_linkOpt){
		$this->linkOpt=$_linkOpt;
	}

	public function set_divsize($_divsize){
		$this->divsize = $_divsize;
	}

	/**
	 * @brief Ajusta los valores de smarty de acuerdo a lo especificado por los atributos de la instancia formatted table
	 */
	private function smarty_formatted_table(){

		global $smarty;
		/**
		 * @brief Asignacion atributo por atributo a lo deseado en las variables de smarty
		 */
		$smarty->assign("arrayTitle",$this->get_titles());
		$smarty->assign("arrayKeys",$this->get_keys());
		$smarty->assign("matrixContent",$this->get_content());
		$smarty->assign("rowOpts",$this->get_opt());
		$smarty->assign("target",$this->get_target());
		$smarty->assign("link",$this->get_link());
		$smarty->assign("nameid",$this->get_nameid());
		$smarty->assign("extraOpt",$this->get_extraOpt());
		$smarty->assign("divsize",$this->get_divsize());
		$smarty->assign("linkOpt",$this->get_linkOpt());

		/**
		 * @brief Asigna la hoja de estilo que contiene los rasgos de la tabla con formato a una variable
		 */
		$smarty->assign("formattedtablecss",FORMATTED_TABLE_CSS);
		$smarty->assign("formattedtablejs",FORMATTED_TABLE_JS);
	}

	/**
	 * @brief Asigna la tabla con formato a la variable $smarty para su renderizacion
	 *
	 */
	public function display_formatted_tables($fetch = false){
		global $smarty;
    $smarty->template_dir = dirname(__FILE__).'/';
		$this->smarty_formatted_table();
		if ($fetch)
		  return $smarty->fetch('formatted_table.tpl');
		else
		  return $smarty->assign( $this->name , $smarty->fetch('formatted_table.tpl') );
	}
}